package deliciouse.servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.ServiceMode;

import deliciouse.dao.UserDao;
import deliciouse.dao.impl.UserDaoImpl;
import deliciouse.entity.Users;

@ WebServlet("/CheckRegister")
public class RegisterCheckServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDao userdao = new UserDaoImpl();
		Users user = new Users();
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		Date lastLoginTime = new Date(0);
		user.setUserName(userName);
		user.setPassword(password);
		user.setLastLoginTime(lastLoginTime);
		if (userdao.userIsExist(userName) == true) {
			request.setAttribute("erro", "用户已经存在！");
			request.getRequestDispatcher("register.jsp").forward(request, response);

		} else {
			userdao.addUser(user);
			response.sendRedirect("login.jsp");
		}
	}
}
